/*
 * io_func.cpp
 *
 * Created: 30.7.2013 22:25:46
 *  Author: Ferda
 */ 
 #include "Configuration.h"
 #include "io_config.h"
 #include <avr/io.h>
 
 //==================================================================================================================
 
 OPTIMIZE_SIZE void io_init()
 {
	 // MAXI SLAVE STEPS ARE OUTPUTS
	 #if IO_CONFIG == PRINTER_MAXI_SLAVE
		SetPinMode(OUT_0_PIN, OUTPUT);
		SetPinMode(OUT_1_PIN, OUTPUT);
		SetPinMode(OUT_2_PIN, OUTPUT);
		SetPinMode(OUT_3_PIN, OUTPUT);
		SetPinMode(OUT_4_PIN, OUTPUT);
		SetPinMode(OUT_5_PIN, OUTPUT);
		SetPinMode(OUT_6_PIN, OUTPUT);
		SetPinMode(OUT_7_PIN, OUTPUT);
		SetPinMode(OUT_8_PIN, OUTPUT);
		SetPinMode(OUT_9_PIN, OUTPUT);
		
		SetPinMode(IN_0_PIN, INPUT);
		SetPinMode(IN_1_PIN, INPUT);
		SetPinMode(IN_2_PIN, INPUT);
		SetPinMode(IN_3_PIN, INPUT);
		SetPinMode(IN_4_PIN, INPUT);
		SetPinMode(IN_5_PIN, INPUT);
		SetPinMode(IN_6_PIN, INPUT);
		SetPinMode(IN_7_PIN, INPUT);
		//SetPinMode(IN_0_PIN, INPUT); SPOLECNY SE VSTUPEM 6
		SetPinMode(IN_9_PIN, INPUT);
	 #else
		SetPinMode(X_STEP_PIN, OUTPUT);
		SetPinMode(Y_STEP_PIN, OUTPUT);
		SetPinMode(Z_STEP_PIN, OUTPUT);
		SetPinMode(E1_STEP_PIN, OUTPUT);
		SetPinMode(E2_STEP_PIN, OUTPUT);
	 
		#ifdef Z_MIN_PIN
			SetPinMode(Z_MIN_PIN, INPUT);
		#elif defined SFSTOP_PIN
			SetPinMode(SFSTOP_PIN, INPUT);
		#else	
			#error "Pin Z_MIN_PIN or SFSTOP_PIN not defined"
		#endif
	
		SetPinMode(SD_EN_PIN, OUTPUT);
		SetPinMode(SD_SS_PIN, OUTPUT);
	
	 
		 
	 PORTB = 0b00011000;	// pull up on AUX0, AUX3 and MISO
	 DDRB = 0b11100111;		// AUX0 and MISO inputs, AUX 3 is output to control led
	 
	 PORTC = 0b11111111;	// pull ups on SD_WP, SD_CD, axis enable bits are ON == drivers off
	 DDRC = 0b00111111;		// SD_WP, SD_CD Inputs, E_HALF and axis enable are outputs
	 
	 PORTD = 0b11111111;	// pull up on rxd, predefine outputs to high
	 DDRD = 0b11111011;		// RXD1 input
	 
	 PORTE = 0b11000011;	// 
	 DDRE = 0b00111100;		// AUX 2/1, PDI, PDO inputs
	 PORTE = 0b11000011;	// 	 
		  
	 PORTF = 0b11111111;	// all pull ups on
	 DDRF = 0b00000000;		// all inputs
	 
	 PORTG = 0b1111111;		// all pull ups on
	 DDRG = 0b00000000;		// ale inputs

	 #endif
	 
	 // init adc
	 DIDR0 = 0b00000111;	// disable digital inputs on first three analog inputs
	 ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
	 ADCSRB = 0;

	 // timer 0 - time measure
	 TCNT0 = 0;
	 OCR0A = 250;								
	 TCCR0A = (1 << WGM01);						// ctc mode
	 TIFR0 = 0;
	 TIMSK0 = (1 << OCIE0A);					
	 TCCR0B = (1 << CS01) | (1 << CS00);		// start timer, prescaler 1/64 = interrupt every 1ms

	 // timer 1 - fans pwm init
	 set_fan1_pwm(0);
	 set_fan2_pwm(0);
	 TCCR1A = (1 << WGM10) | (1 << COM1A1) | (1 << COM1B1);					// mode 2, clera pin on compare match on two pins
	 TCCR1B = (1 << WGM12) | (1 << CS11) | (1 << CS10);						// prescaller 1/64, fast pwm, mode 5, 8bit

	 // 3 - heaters
	 set_heater1_pwm(0);
	 set_heater2_pwm(0);
	 set_bed_heater_pwm(0);
	 TCCR3A = (1 << WGM30) | (1 << COM3A1) | (1 << COM3B1) | (1 << COM3C1); // mode 2, clear pin on compare match on three pins
	 TCCR3B = (1 << WGM32) | (1 << CS31) | (1 << CS30);						// prescaler 1/64, fast pwm, mode 5, 8bit

	#if SYSTEM_CONFIG != 4
	 // 4 - steps generator
	 TCCR4A = 0;
	 TCCR4B = (1 << WGM42) | (1 << CS41);									// 2MHz, CTC mode
	 TCNT4 = 0;
	 OCR4A = 2000;
	#endif
	 
 }

 //========================================================================================
 void io_uninit()
 {
	 DDRA = 0;
	 DDRB = 0;
	 DDRC = 0;
	 DDRD = 0;
	 DDRE = 0;
	 DDRF = 0;
	 DDRG = 0;

	 // adc uninit
	 ADCSRA = 0;
	 DIDR0 = 0;

	 // timer 0 - time measure

	 // timer 1 - fans uninit
	 TCCR1A = TCCR1B = TCCR1C = 0;

	 // timer 3 - heaters uninit
	 TCCR3A = TCCR3B = TCCR3C = 0;

	 // timer 4 - step generator interrupt
 }
 //========================================================================================

